﻿//-----------------------------------------------------------------------
// <copyright file="AacpFrameParser.cs" company="Andrew Oakley">
//     Copyright (c) 2010 Andrew Oakley
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU Lesser General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
//
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU Lesser General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General Public License
//     along with this program.  If not, see http://www.gnu.org/licenses.
// </copyright>
//-----------------------------------------------------------------------

namespace Silverlight.Media.Parsers
{
    using System;

    /// <summary>
    /// Class used to parse AAC+ audio frame headers.
    /// </summary>
    public class AacpFrameParser : AudioFrameParser
    {
        /// <summary>
        /// Initializes a new instance of the AacpFrameParser class.
        /// </summary>
        public AacpFrameParser()
            : base()
        {
            this.FrameHeaderSize = AacpFrame.FrameHeaderSize;
            this.SyncBytes = AacpFrame.SyncBytes;
        }

        /// <summary>
        /// Checks the supplied bytes to verify it is a valid AAC+ audio frame.
        /// </summary>
        /// <param name="audioFrameHeader">Bytes representing an AAC+ audio frame header.</param>
        /// <returns>True if the supplied bytes are a valid AAC+ audio frame, otherwise, false.</returns>
        public override bool IsValidFrame(byte[] audioFrameHeader)
        {
            return AacpFrame.IsValidFrame(audioFrameHeader);
        }

        /// <summary>
        /// Creates an AacpFrame from the supplied audio frame header bytes.
        /// </summary>
        /// <param name="audioFrameHeader">Bytes representing an AAC+ audio frame header.</param>
        /// <returns>Instance of an AacpFrame.</returns>
        public override AudioFrame CreateFrame(byte[] audioFrameHeader)
        {
            return new AacpFrame(audioFrameHeader);
        }

        /// <summary>
        /// Creates a WaveFormat based on the supplied AudioFrame.
        /// </summary>
        /// <param name="audioFrame">An AudioFrame instance used to create the WaveFormat.</param>
        /// <returns>Instance of a WaveFormat.</returns>
        public override WaveFormat CreateWaveFormat(AudioFrame audioFrame)
        {
            if (audioFrame == null)
            {
                throw new ArgumentNullException("audioFrame");
            }

            WaveFormatExtensible wfx = new WaveFormatExtensible();

            wfx.FormatTag = 0x1610;
            wfx.Channels = (short)audioFrame.NumberOfChannels;
            wfx.SamplesPerSec = audioFrame.SamplingRate;
            wfx.AverageBytesPerSecond = audioFrame.BitRate / 8;
            wfx.BlockAlign = 1;
            wfx.BitsPerSample = 0;
            wfx.Size = 12;

            HeAacWaveFormat aacf = new HeAacWaveFormat(wfx);

            // Extra 3 words in WAVEFORMATEX
            aacf.PayloadType = 0x1; // Audio Data Transport Stream (ADTS). The stream contains an adts_sequence, as defined by MPEG-2.
            aacf.AudioProfileLevelIndication = 0xFE;
            aacf.StructType = 0;

            return aacf;
        }
    }
}
